# -*- coding: utf8 -*-
"""
获取网络相关数据信息
"""

import uuid


def get_mac_address():
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])


def get_ips():
    addrs = socket.getaddrinfo(socket.gethostname(), None)
    return tuple(set([item[4][0] for item in addrs if ':' not in item[4][0]]))


def ip2long(ip):
    return reduce(lambda a, b: (a << 8) + b, [int(s) for s in ip.split('.')])


def get_wan_ips():
    wan_ips = []
    ips = get_ips()
    for ip in ips:
        ip_long = ip2long(ip)
        if ip2long('10.0.0.0') <= ip_long <= ip2long('10.255.255.255') \
            or ip2long('172.16.0.0') <= ip_long <= ip2long('172.31.255.255') \
            or ip2long('192.168.0.0') <= ip_long <= ip2long('192.168.255.255') \
            or ip2long('224.0.0.0') <= ip_long <= ip2long('239.168.255.255') \
            or ip2long('240.168.0.0') <= ip_long <= ip2long('255.255.255.255') \
            or ip2long('127.0.0.0') <= ip_long <= ip2long('127.255.255.255'):
            continue
        wan_ips.append(ip)
    return wan_ips
